﻿using System.Runtime.CompilerServices;
using System.Text;

namespace ServiceCore.Utility.Extension
{
    public static class HighPerformanceChineseParser
    {
        // 使用Span和内存优化
        public static string ParseNumbersFast(ReadOnlySpan<char> input, string endChar = "", string beforeChar = "")
        {
            var result = new StringBuilder(input.Length * 2);
            int i = 0;

            while (i < input.Length)
            {
                // 查找数字模式
                if (IsChineseDigit(input[i]))
                {
                    int start = i;
                    while (i < input.Length && IsChineseDigit(input[i])) i++;

                    var numberSpan = input.Slice(start, i - start);
                    string parsed = ParseNumberSpan(numberSpan, endChar, beforeChar);
                    result.Append(parsed);
                }
                else
                {
                    result.Append(input[i]);
                    i++;
                }
            }

            return result.ToString();
        }
        /// <summary>
        /// 使用switch表达式进行快速字符判断（性能最优）
        /// </summary>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static bool IsChineseDigit(char c)
        {
            return c switch
            {
                '〇' or '一' or '二' or '三' or '四' or '五' or '六' or '七' or '八' or '九' => true,
                '十' or '百' or '千' or '万' or '亿' => true,
                '零' or '壹' or '贰' or '叁' or '肆' or '伍' or '陆' or '柒' or '捌' or '玖' => true,
                '拾' or '佰' or '仟' => true,
                _ => false
            };
        }

        /// <summary>
        /// 高性能检查字符串中是否包含中文数字字符
        /// </summary>
        public static bool HasChineseNumber(this string input)
        {
            if (string.IsNullOrWhiteSpace(input))
                return false;

            foreach (char c in input)
            {
                if (IsChineseDigit(c))
                    return true;
            }
            return false;
        }

        private static string ParseNumberSpan(ReadOnlySpan<char> numberSpan, string endChar = "", string beforeChar = "")
        {
            if (numberSpan.Length == 1)
            {
                return numberSpan[0] switch
                {
                    '零' or '〇' => "0",
                    '一' => "1",
                    '二' => "2",
                    '三' => "3",
                    '四' => "4",
                    '五' => "5",
                    '六' => "6",
                    '七' => "7",
                    '八' => "8",
                    '九' => "9",
                    '十' => "10",
                    _ => beforeChar + numberSpan.ToString() + endChar
                };
            }

            // 简单处理复合数字
            long result = 0;
            long current = 0;

            foreach (char c in numberSpan)
            {
                long value = c switch
                {
                    '零' or '〇' => 0,
                    '一' => 1,
                    '二' => 2,
                    '三' => 3,
                    '四' => 4,
                    '五' => 5,
                    '六' => 6,
                    '七' => 7,
                    '八' => 8,
                    '九' => 9,
                    '十' => 10,
                    '百' => 100,
                    '千' => 1000,
                    '万' => 10000,
                    '亿' => 100000000,
                    _ => 0
                };

                if (value < 10)
                {
                    current = value;
                }
                else if (value < 10000)
                {
                    if (current == 0) current = 1;
                    result += current * value;
                    current = 0;
                }
                else
                {
                    result = (result + current) * value;
                    current = 0;
                }
            }

            return beforeChar + (result + current).ToString() + endChar;
        }
    }
}
